<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="考试名称" prop="examName">
        <el-input v-model="dataForm.examName" placeholder="考试名称"></el-input>
      </el-form-item>
      <el-form-item label="题目数量" prop="examCount">
        <el-input
          v-model="dataForm.examCount"
          placeholder="题目数量"
        ></el-input>
      </el-form-item>
      <el-form-item label="考试总分" prop="examSum">
        <el-input v-model="dataForm.examSum" placeholder="考试总分"></el-input>
      </el-form-item>
      <el-form-item label="考试封面" prop="examHover">
        <el-input
          v-model="dataForm.examHover"
          placeholder="考试封面"
        ></el-input>
      </el-form-item>
      <el-form-item label="发布讲师" prop="teacherId">
        <el-input
          v-model="dataForm.teacherId"
          placeholder="发布讲师"
        ></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-input
          v-model="dataForm.startTime"
          placeholder="开始时间"
        ></el-input>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-input v-model="dataForm.endTime" placeholder="结束时间"></el-input>
      </el-form-item>
      <el-form-item label="考试时间" prop="examTime">
        <el-input v-model="dataForm.examTime" placeholder="考试时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        examName: "",
        examCount: "",
        examSum: "",
        examHover: "",
        teacherId: "",
        startTime: "",
        endTime: "",
        examTime: "",
        createTime: "",
        updateTime: "",
        isDelete: "",
      },
      dataRule: {
        examName: [
          { required: true, message: "考试名称不能为空", trigger: "blur" },
        ],
        examCount: [
          { required: true, message: "题目数量不能为空", trigger: "blur" },
        ],
        examSum: [
          { required: true, message: "考试总分不能为空", trigger: "blur" },
        ],
        examHover: [
          { required: true, message: "考试封面不能为空", trigger: "blur" },
        ],
        teacherId: [
          { required: true, message: "发布讲师不能为空", trigger: "blur" },
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
        ],
        examTime: [
          { required: true, message: "考试时间不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: false, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: false, message: "修改时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/exam/exam/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.examName = data.exam.examName;
              this.dataForm.examCount = data.exam.examCount;
              this.dataForm.examSum = data.exam.examSum;
              this.dataForm.examHover = data.exam.examHover;
              this.dataForm.teacherId = data.exam.teacherId;
              this.dataForm.startTime = data.exam.startTime;
              this.dataForm.endTime = data.exam.endTime;
              this.dataForm.examTime = data.exam.examTime;
              this.dataForm.createTime = data.exam.createTime;
              this.dataForm.updateTime = data.exam.updateTime;
              this.dataForm.isDelete = data.exam.isDelete;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam/exam/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              examName: this.dataForm.examName,
              examCount: this.dataForm.examCount,
              examSum: this.dataForm.examSum,
              examHover: this.dataForm.examHover,
              teacherId: this.dataForm.teacherId,
              startTime: this.dataForm.startTime,
              endTime: this.dataForm.endTime,
              examTime: this.dataForm.examTime,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              isDelete: this.dataForm.isDelete,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
